package com.wuge.gulimall.product.Exception;

import com.wuge.common.utils.R;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

@RestControllerAdvice
public class ProductException {
      @ExceptionHandler(MethodArgumentNotValidException.class)
    public R validExceptionHandler(MethodArgumentNotValidException m){
        HashMap map=new HashMap();

        BindingResult bindingResult = m.getBindingResult();
        bindingResult.getFieldErrors().forEach((filed)->{
            map.put(filed.getField(),filed.getDefaultMessage());
        });
        return R.error(400,"数据校验出错").put("data",map);

    }
    @ExceptionHandler(Throwable.class)
    public R ExceptionHandler(Throwable m){


        return R.error(500,m.getMessage());

    }
}
